/**				servlet is the entrance of program
 * logic servlet is the entrance of business system
 * servlet layer implements verification of parameter
 * response encapsulated in business module due to content maybe text or image
 * by fuzhou huilin corp. ltd @author lms 2016.08.26
 */
package com.fzrskj.cloud.free.core.servlet;

import com.fzrskj.cloud.free.core.tool.CCookieTool;
import com.fzrskj.cloud.free.core.tool.CEncryptTool;
import com.fzrskj.cloud.free.core.tool.CResponseTool;
import com.fzrskj.cloud.free.core.tool.CStrTool;
import ms.core.consts.SysErr;
import ms.core.tool.ReflectTool;
import ms.core.tool.XmlCodeParser;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CoreServlet extends HttpServlet {
	//need to be removed, by lms 2016.07.18
	private static boolean debug=true;

	//servlet serial id
	private static final long serialVersionUID = 3000L;

	//constructor function, you can do some initialize here
	public CoreServlet() {
		super();
	}

	//Destruction of the servlet. <br>
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**to verify the parameter legal
	 * @param request client request
	 * @return true for legal parameter or false for illegal parameter
	 */
	private boolean validRequestParameter(HttpServletRequest request){
		if (debug)
			return true;

		String f = request.getParameter("fn");
		if (CStrTool.isBlankStr(f))
			return false;

		String p = CStrTool.getDefaultValue(request.getParameter("p"),"");
		String t = CStrTool.getDefaultValue(request.getParameter("t"),"");
		String s = CStrTool.getDefaultValue(request.getParameter("sign"),"");

		return (CStrTool.isBlankStr(p) && CStrTool.isBlankStr(t)) ||
			   (CEncryptTool.md5Encode(p+t).equals(s));
	}

	/**the entrance of system servlet with get method
	 * This method is called when a form has its tag value method equals to get.
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	/**the request url is like this: http://host/sys.do?fn=xxx&...
	 * The doPost method of the servlet. <br>
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String sid = CCookieTool.getSessionId(request);
		//allocate session id to client
		CCookieTool.refreshSessionId(request, response, sid);
		if (!validRequestParameter(request)){
			String ret = XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
			CResponseTool.doResponseText(response, ret);
			return;
		}

		String f = request.getParameter("fn");
		String p = request.getParameter("p");
		String c = request.getParameter("callback");
		String content = ReflectTool.call(0, request, response, f, p, sid);
		if (!CStrTool.isBlankStr(content)){
			if (!CStrTool.isBlankStr(c))
				content = String.format("%s(%s)", c, content);	//convert to json p
			CResponseTool.doResponseText(response, content);
		}
	}

	/**Initialization of the servlet. <br>
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		//TODO Put your code here
	}

}
